#
# Copyright 2009 Jeffrey Palm
#

require 'prints_help_impl'

# This class can parse a command line and set HasOptions
class ParseArgs

  attr_reader :prints_help

  def initialize(prints_help=nil)
    @prints_help = (prints_help ? prints_help : PrintsHelpImpl.new)
  end

  # Parse the command line arguments and set the appropriate values of
  # the passed in HasOptions.  Return 0 or a value from the RET_
  # values.
  def parse_args(args,has_options)
    real_args = []
    args.each do |arg|
      if arg == '-r' || arg =~ /-resume/
        has_options.resume = true 
      elsif arg == '-p' || arg =~ /-pause/
        has_options.pause = true 
      elsif arg == '-c' || arg =~ /-check/
        has_options.just_check = true
      elsif arg == '-n' || arg =~ /-motions/
        has_options.motions = true
      elsif arg == '-b' || arg =~ /-verbose/
        has_options.verbose = true
      elsif arg == '-h' || arg =~ /-help/
        prints_help.print_help
        return RET_PRINTED_HELP
      elsif arg == '-d' || arg =~ /-deletesharedkey/
        remove_shared_key = true
      elsif arg =~ /^-/
        note 'Invalid argument ' + arg
      else
        real_args << arg
      end
    end
    
    # The shared key may be passed on the command line, it'd be the
    # only argument left standing
    if not real_args.empty?
      note 'Have a shared key on the command line'
      has_options.shared_key = real_args.shift
    end

  end

end
